<?php

use yii\helpers\Html;
use yii\helpers\Url;
use kartik\editable\Editable;
use kartik\export\ExportMenu;
use kartik\dynagrid\DynaGrid;
use kartik\grid\GridView;
use kartik\ipinfo\IpInfo;
use kartik\popover\PopoverX;
use kartik\daterange\DateRangePicker;
use \kartik\field\FieldRange;

/**
 * @var yii\web\View $this
 * @var yii\data\ActiveDataProvider $dataProvider
 * @var common\models\search\DemoSearch $searchModel
 */

$this->title = Yii::t('models', 'Demos');
$this->params['breadcrumbs'][] = $this->title;

if (isset($actionColumnTemplates)) {
    $actionColumnTemplate = implode(' ', $actionColumnTemplates);
    $actionColumnTemplateString = $actionColumnTemplate;
} else {
    Yii::$app->view->params['pageButtons'] = Html::a('<span class="glyphicon glyphicon-plus"></span> ' . Yii::t('cruds', 'New'), ['create'], ['class' => 'btn btn-success']);
    $actionColumnTemplateString = "{view} {update} {delete}";
}
$actionColumnTemplateString = '<div class="action-buttons">' . $actionColumnTemplateString . '</div>';
?>
<div class="giiant-crud demo-index">

    <?php
    // echo $this->render('_search', ['model' =>$searchModel]);
    ?>

    <h1>
        <?= Yii::t('models', 'Demos') ?>
        <small>
            List
        </small>
    </h1>
    <div class="clearfix crud-navigation">
        <div class="pull-left">
            <?= Html::a('<span class="glyphicon glyphicon-plus"></span> ' . Yii::t('cruds', 'New'), ['create'], ['class' => 'btn btn-success']) ?>
        </div>

        <div class="pull-right">

            <?=
            \yii\bootstrap\ButtonDropdown::widget(
                [
                    'id' => 'giiant-relations',
                    'encodeLabel' => false,
                    'label' => '<span class="glyphicon glyphicon-paperclip"></span> ' . Yii::t('cruds', 'Relations'),
                    'dropdown' => [
                        'options' => [
                            'class' => 'dropdown-menu-right'
                        ],
                        'encodeLabels' => false,
                        'items' => [
                            [
                                'url' => ['demo-item/index'],
                                'label' => '<i class="glyphicon glyphicon-arrow-right"></i> ' . Yii::t('models', 'Demo Item'),
                            ],

                        ]
                    ],
                    'options' => [
                        'class' => 'btn-default'
                    ]
                ]
            );
            ?>
        </div>
    </div>

    <hr/>

    <?php

    $columns = [
        ['class' => 'kartik\grid\SerialColumn', 'order' => DynaGrid::ORDER_FIX_LEFT],
        ['class' => 'kartik\grid\CheckboxColumn', 'order' => DynaGrid::ORDER_FIX_LEFT],
        [
            'class' => 'kartik\grid\ActionColumn',
            'dropdown' => false,
            'contentOptions' => ['class' => 'text-center operation'],
            'headerOptions' => ['style' => 'min-width:100px'],
            'order' => DynaGrid::ORDER_FIX_LEFT
        ],

//            ['class' => 'kartik\grid\ActionColumn', 'header' => Yii::t('app', 'action'), 'order' => DynaGrid::ORDER_FIX_LEFT,
//                'template' => '{delete}　{update}',
//                'buttons' => [
//                    'update' => function ($url, $model, $key) {
//                      return Html::a(Html::tag('span', '', ['class' => '']), ['orders/update'], ['class' => "", 'title' => 'test']);
//                    },
//                    'delete' => function ($url, $model, $key) {
//
//                        return Html::a('', 'javascript:;', ['title' => Yii::t('app', 'cancel_order'), 'pjax' => 0, 'class' => 'tooltips', 'data-placement' => 'bottom', 'data-container' => 'body',]);
//                    },
//                ]
//            ],

        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'password',
            'filterType' => GridView::FILTER_TYPEAHEAD,
            'filterWidgetOptions' => [
                'pluginOptions' => [
                    'highlight' => true,
                ],
                'options'       => [
                    'placeholder'  => Yii::t('app', 'Filter as you type') . ' ...',
                    'autocomplete' => 'off',
                ],
                'dataset'       => [
                    [
                        'datumTokenizer' => 'Bloodhound.tokenizers.obj.whitespace("value")',
                        'display'        => 'value',
                        'remote'         => [
                            'url'      => Url::to(['typehead', 'attribute' => 'password']) . '&q=%QUERY',
                            'wildcard' => '%QUERY',
                        ],
                        'templates'      => [
                            'notFound' => '<div class="text-danger" style="padding:0 8px">' . Yii::t('app', 'No results') . '</div>',
                        ],
                    ],
                ],
            ],
            // 'filter' => ['333', '4444'],
            'editableOptions' => [
                'asPopover' => true,
            ],

        ],

        [
            'attribute' => 'password_repeat',
//            'pageSummaryFunc' => GridView::F_SUM,
//            'pageSummary' => true,
            //  'header' => Yii::t('app', 'prop_type_name'),

            'filterType' => GridView::FILTER_SELECT2,
            'filter' => [1 => '333', 2 => '4444'],

            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => Yii::t('app', 'please_select'), 'id' => 'grid-props-search-type_id']
        ],

        [
            'class' => 'kartik\grid\EditableColumn',

            'filterType' => GridView::FILTER_DATE_RANGE,
            'filterWidgetOptions' => [
                'attribute' => 'date',
                'convertFormat' => true,
                'pluginOptions' => [
                    'locale' => [
                        'format' => 'Y-m-d',
                    ],
                    'pluginOptions' => ['allowClear' => true],
                ],
            ],


            'attribute' => 'date',
            'hAlign' => 'center',
            'vAlign' => 'middle',
            'width' => '9%',
            'format' => 'date',
            'xlFormat' => "mmm\\-dd\\, \\-yyyy",
            'headerOptions' => ['class' => 'kv-sticky-column', 'style' => 'min-width:100px'],
            'contentOptions' => ['class' => 'kv-sticky-column'],
//                'readonly' => function($model, $key, $index, $widget) {
//                    return (!$model->status); // do not allow editing of inactive records
//                },
            'editableOptions' => [
                'header' => 'Publish Date',
                'size' => 'md',
                'inputType' => \kartik\editable\Editable::INPUT_WIDGET,
                'widgetClass' => 'kartik\datecontrol\DateControl',
                'options' => [
                    'type' => \kartik\datecontrol\DateControl::FORMAT_DATE,
                    'displayFormat' => 'dd.MM.yyyy',
                    'saveFormat' => 'php:Y-m-d',
                    'options' => [
                        'pluginOptions' => [
                            'autoclose' => true
                        ]
                    ]
                ]
            ],
        ],


        [
            'class' => 'kartik\grid\EditableColumn',

            'attribute' => 'date_time',
            'hAlign' => 'center',
            'vAlign' => 'middle',
            // 'width' => '9%',
            'format' => 'date',
            'xlFormat' => "mmm\\-dd\\, \\-yyyy",
            'headerOptions' => ['class' => 'kv-sticky-column', 'style' => 'min-width:250px'],
            'filterType' => GridView::FILTER_DATETIME,
            'contentOptions' => ['class' => 'kv-sticky-column'],
//                'readonly' => function($model, $key, $index, $widget) {
//                    return (!$model->status); // do not allow editing of inactive records
//                },
            'editableOptions' => [
                'header' => 'Publish Date',
                'size' => 'md',
                'inputType' => \kartik\editable\Editable::INPUT_WIDGET,
                'widgetClass' => 'kartik\datecontrol\DateControl',
                'options' => [
                    'type' => \kartik\datecontrol\DateControl::FORMAT_DATE,
                    'displayFormat' => 'dd.MM.yyyy',
                    'saveFormat' => 'php:Y-m-d',
                    'options' => [
                        'pluginOptions' => [
                            'autoclose' => true
                        ]
                    ]
                ]
            ],
        ],


        [
            'headerOptions' => ['style' => 'min-width:180px'],
            'attribute' => 'date_time',
            'format' => 'date',
            'filter' => \kartik\daterange\DateRangePicker::widget([
                'model' => $searchModel,
                'attribute' => 'date_time',
                'convertFormat' => true,
                'startAttribute' => 'amount_final_min',
                'endAttribute' => 'amount_final_max',
                //'hideInput'=>true,
                'presetDropdown' => true,
                'pluginOptions' => [

                    'locale' => [
                        'format' => 'Y-m-d',
                        'separator' => ' TO '
                    ],
                ],
            ]),
        ],


        [
            'class' => '\kartik\grid\DataColumn',
            'attribute' => 'double_value',
            'format' => 'currency',
            'pageSummaryFunc' => GridView::F_SUM,
            'pageSummary' => true,
        ],

        'email:email',

        [
            'headerOptions' => ['class' => 'kv-sticky-column', 'style' => 'min-width:300px'],
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'status',
            'editableOptions' => [
                    'data'=>[1=>'bbb',2=>'cc'],
                'inputType' => Editable::INPUT_DROPDOWN_LIST,


            ],

        ],


        [
                'attribute'=>'ip',
        ],
        [
            'attribute' => 'ip',
//            'value'=>function(){
//
//              return  IpInfo::widget([
//                    'ip' => Yii::$app->request->userIP,
//                  'template' => ['popoverButton'=>'{flag} ({city}, {region} ,{country})'],
//                    'popoverOptions' => [
//                        'toggleButton' => ['class' => 'btn btn-secondary btn-lg'],
//                        'placement' => PopoverX::ALIGN_BOTTOM_LEFT
//                    ]
//                ]);
//            },

            'format' => 'raw'
        ],

        [
            'attribute' => 'number',
            // 'headerOptions' => ['class' => 'kv-sticky-column', 'style' => 'min-width:100px'],
            //  'filterType' => GridView::FILTER_NUMBER,

            'headerOptions' => ['style' => 'min-width:180px'],
            'filter' => FieldRange::widget([
                'separator' => '~',
                'model' => $searchModel,
                'attribute1' => 'amount_final_min',
                'attribute2' => 'amount_final_max',
                'type' => FieldRange::INPUT_TEXT,
                'template' => '{widget}',
                'options1' => [
                    'type' => 'text',
                    'min' => 0,
                ],
                'options2' => [
                    'type' => 'text',
                ],
            ]),
        ],
        'url:url',

        [
            'headerOptions' => ['class' => 'kv-sticky-column', 'style' => 'min-width:300px'],
            'attribute' => 'content',
            'value' => function ($model, $key) {

                return mb_substr($model->content, 0, 10);
            },
            'class' => 'kartik\grid\EditableColumn',

            'editableOptions' => [
                'inputType' => Editable::INPUT_TEXTAREA,
                'size'=>'lg',
                'options' => ['class'=>'form-control', 'rows'=>5, 'placeholder'=>'Enter notes...']
            ],
        ],

        //'json_value',
        // 'zip_code',


        [
            'attribute' => 'zip_code',
        ],



        [
            'headerOptions' => ['class' => 'kv-sticky-column', 'style' => 'min-width:300px'],
            'class' => 'kartik\grid\EditableColumn',
            'attribute'=>'address',

        ],
        //'mobile',

        //  FILTER_RANGE

        [
            'headerOptions' => ['class' => 'kv-sticky-column', 'style' => 'min-width:100px'],
            'class' => '\kartik\grid\DataColumn',
            'attribute' => 'mobile',
            'pageSummaryFunc' => GridView::F_SUM,
            'pageSummary' => true,
	],


        [
            'headerOptions' => ['class' => 'kv-sticky-column', 'style' => 'min-width:300px'],
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'chinese_string',

        ],

        [
            'headerOptions' => ['class' => 'kv-sticky-column', 'style' => 'min-width:100px'],
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'image',
            'editableOptions' => [
                'formOptions' => [
                    'action' => ['upload'],
                ],
                'preHeader' => '',

                'inputType' => Editable::INPUT_FILEINPUT,
                'options' => [
                    'options' => ['accept' => 'image/*'],
                    'pluginOptions' => [
                        'showRemove' => false,
                        'showUpload' => false
                    ]
                ],
            ],


//            'value'=>function($model){
//
//               $frontend = Yii::getAlias('@frontendUrl');
//                $images =\yii\helpers\Json::decode($model->image);
//                return Html::img($frontend.'/img/'.$images[0],['width'=>'100px','height'=>'100px']);
//            },
            'format'=>'raw'
        ],


        [
            'headerOptions' => ['class' => 'kv-sticky-column', 'style' => 'min-width:100px'],
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'file',
            'editableOptions' => [
                'formOptions' => [
                    'action' => ['upload'],
                ],
                'preHeader' => '',

                'inputType' => Editable::INPUT_FILEINPUT,
                'options' => [
                    'options' => ['accept' => '*'],
                    'pluginOptions' => [
                        'showRemove' => false,
                        'showUpload' => false
                    ]
                ],
            ],


//            'value'=>function($model){
//
//                $frontend = Yii::getAlias('@frontendUrl');
//                $images =\yii\helpers\Json::decode($model->file);
//                return $frontend.'/img/'.$images[0];
//            },
            'format'=>'raw'
        ],

        'title',
        'slug',

        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'boolean',
//            'filterType' => GridView::FILTER_CHECKBOX_X,
//            'filter' => ['no', 'yes'],
//            'filterInputOptions' => ['placeholder' => Yii::t('app', 'please_select')],
//            'filterWidgetOptions' => [
//                'pluginOptions' => ['allowClear' => true],
//            ],

            'editableOptions' => [
                'inputType' => Editable::INPUT_DROPDOWN_LIST,
                'data' => [1, 2],
                'displayValueConfig' => ['no', 'yes'],
                'asPopover' => false,
            ],
            'format' => 'boolean'
        ],

        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'star',

            'editableOptions' => [
                'inputType' => Editable::INPUT_RATING,
            ],
        ],



        [
            'attribute' => 'star',
            'headerOptions' => ['class' => 'kv-sticky-column', 'style' => 'min-width:100px'],
            'pageSummaryFunc' => GridView::F_COUNT,
            'pageSummary' => true,
            'group' => true,  // enable grouping
            'groupedRow' => false,
            'groupFooter' => function ($model, $key, $index, $widget) { // Closure method

//                return [
//                    'mergeColumns'=>[[2,2]], // columns to merge in summary
//                    'content' => [             // content to show in each summary cell
//                        1 => "Summary [{$model->star}]",
//                        3 => GridView::F_COUNT,
//                    //    5 => GridView::F_AVG,
//                       // 5 => "Balance",
//                      // 5=>GridView::F_AVG,
////                        4=>GridView::F_SUM,
//                    ],
//                    'contentFormats' => [      // content reformatting for each summary cell
//                        3 => ['format' => 'number', 'decimals' => 0],
//                        5=>['format'=>'number', 'decimals'=>2],
//                        //6=>['format'=>'number', 'decimals'=>2],
//                    ],
//                    'contentOptions' => [      // content html attributes for each summary cell
//                        1 => ['style' => 'font-variant:small-caps'],
//                        2 => ['style' => 'text-align:right'],
//                        3 => ['style' => 'text-align:right'],
//                        4 => ['style' => 'font-variant:small-caps'],
//                        5 => ['style' => 'font-variant:small-caps'],
//                        //6=>['style'=>'text-align:right'],
//                    ],
//                    // html attributes for group summary row
//                    'options' => ['class' => 'success', 'style' => 'font-weight:bold;']
//                ];
            }
        ],

    ];

    $export_file_name = $this->title . '-' . date("d-m-Y-H-i-s");
    $export_columns = $columns;
    foreach ($export_columns as $k => &$v) {
        if (isset($v['order'])) unset($v['order']);
    }
    $fullExportMenu = ExportMenu::widget([
        'dataProvider' => $dataProvider,
        'columns' => $export_columns,
        'fontAwesome' => true,
        'noExportColumns' => [1],
//            'batchSize' => 10,
        'stream' => false,
        //'target' => '_blank',
        'linkPath' => '/tmp',
        'folder' => "@webroot/tmp",
        'filename' => $export_file_name,

//            'exportConfig' => [
//                ExportMenu::FORMAT_TEXT => false,
//                ExportMenu::FORMAT_PDF => false,
//                ExportMenu::FORMAT_HTML => false,
//                ExportMenu::FORMAT_CSV => false,
//            ],
    ]);

    echo DynaGrid::widget([
        'columns' => $columns,
        'storage' => DynaGrid::TYPE_COOKIE,
        'theme' => 'panel-info',
        'gridOptions' => [
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'panel' => ['heading' => '<h3 class="panel-title">Library</h3>'],
            'toolbar' => [
                ['content' => '{dynagrid}',],
                $fullExportMenu
            ],
            //  'perfectScrollbar' => true,
            'perfectScrollbarOptions' => [
                'wheelPropagation' => true,
            ],
            // 'floatHeader' => true,
            'showPageSummary' => true,
        ],
        'showPersonalize' => true,
        'options' => ['id' => 'dynagrid-1'], // a unique identifier is important
    ]);

    ?>
</div>





